package org.budo.cmbc.direct.sdk.entity.ryb;

import java.util.ArrayList;
import java.util.List;

import org.budo.cmbc.direct.sdk.entity.AbstractCmbcDirectResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * 风评题目查询
 */
@Getter
@Setter
@Accessors(chain = true)
@NoArgsConstructor
public class MERiskQuestionQryResponse extends AbstractCmbcDirectResponse {
    private static final long serialVersionUID = -2828530797519443365L;

    /**
     * 题目列表
     */
    private List<MERiskQuestion> questionsList;

    public MERiskQuestionQryResponse(String channelJnlNo, String responseJson) {
        super(channelJnlNo, responseJson);

        this.questionsList = new ArrayList<>();

        JSONObject jsonObj = JSON.parseObject(responseJson);

        JSONArray questionArr = jsonObj.getJSONArray("QuestionsList");

        if (questionArr != null && questionArr.size() > 0) {
            for (int i = 0; i < questionArr.size(); i++) {
                JSONObject questionObj = questionArr.getJSONObject(i);

                MERiskQuestion question = new MERiskQuestion();
                question.setQuestion(questionObj.getString("Question"));

                JSONArray answerArr = questionObj.getJSONArray("AnswersList");

                List<MERiskQuestionAnswer> answerList = new ArrayList<>();

                if (answerArr != null && answerArr.size() > 0) {

                    for (int j = 0; j < answerArr.size(); j++) {

                        JSONObject answerObj = answerArr.getJSONObject(j);

                        MERiskQuestionAnswer answer = new MERiskQuestionAnswer();
                        answer.setAnswerContent(answerObj.getString("AnswerContent"));
                        answer.setSelection(answerObj.getString("Selection"));

                        answerList.add(answer);
                    }
                }

                question.setAnswersList(answerList);

                this.questionsList.add(question);
            }
        }
    }
}
